package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractProduct;
import cn.itcast.domain.cargo.ContractProductExample;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.utils.UUIDUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service //dubbo注解
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    ContractProductDao contractProductDao;

    @Autowired
    ContractDao contractDao;

    @Override
    public void save(ContractProduct contractProduct) {
        //1.* 向货物表添加一条数据
        //a.设置货物的id
        contractProduct.setId(UUIDUtils.getId());
        //b.计算货物的小计
        Double amount = 0d;
        if (contractProduct.getCnumber()!=null && contractProduct.getPrice()!=null) {
            amount = contractProduct.getCnumber()*contractProduct.getPrice();
        }
        //c.设置货物的小计
        contractProduct.setAmount(amount);
        //d.保存货物
        contractProductDao.insertSelective(contractProduct);

        //2.* 修改合同中的数据
        //a.获取合同对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //b.更新合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //c.更新货物的总数量  +1
        contract.setProNum(contract.getProNum() + 1);
        //d.更新合同信息
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //1.更新货物表中的数据
        //a.获取货物对象,获取原来的小计
        Double oldAmount = contractProductDao.selectByPrimaryKey(contractProduct.getId()).getAmount();
        //b.设置最新的小计
        Double newAmonut = 0d;
        if (contractProduct.getCnumber()!=null && contractProduct.getPrice()!=null) {
            newAmonut = contractProduct.getCnumber()*contractProduct.getPrice();
        }
        contractProduct.setAmount(newAmonut);
        //c.更新货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);

        //2.更新合同表中的数据
        //a.获取合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //b.修改合同的总金额 = 原来总金额 - 修改货物的原来金额 + 最新的金额
        contract.setTotalAmount(contract.getTotalAmount() - oldAmount + newAmonut);
        //c.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void deleteById(String id) {
        //1.从货物表中删除一条记录
        //a.获取货物对象,
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //b.获取小计
        Double amount = contractProduct.getAmount();
        //c.调用dao完成删除
        contractProductDao.deleteByPrimaryKey(id);

        //2.更新合同表
        //a.获取合同对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //b.设置货物数量 -1
        contract.setProNum(contract.getProNum() - 1);
        //c.设置总金额  = 原来的金额- 删除货物的小计
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        //d.更新合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(int page, int size, ContractProductExample example) {
        PageHelper.startPage(page,size);
        return new PageInfo(contractProductDao.selectByExample(example));
    }

    @Override
    public void batchSave(List<ContractProduct> list) {
        if (list!=null && list.size()>0) {
            for (ContractProduct contractProduct : list) {
                save(contractProduct);
            }
        }
    }
}
